var _shopId;
var _openId;
$(function() {
	_shopId = qmsk.getQueryValue("shopId");
	_openId = wx.getOpenId();
	search();
	InitCount();
});

var _goodsMedId = '';

function search() {
	$("#emptyDiv").hide();
	$("#goodsList").empty();
	new qmsk.SiglePinterest({
		method : 'get',
		url : '../goods/find/shopId/' + _shopId + '/opt/',
		data : {
			'goodsMedTypeId' : _goodsMedId
		},
		initPage : 3,
		size : 4,
		success : printDataToPage,
		valves : 100,
		emptyHandler : emptyHandler,
		loading : addRefresh,
		loaded : delRefresh
	});
}

function emptyHandler() {
	$("#emptyDiv").show();
}

function printDataToPage(response) {
	var row = response.row;
	for (var i = 0; i < row; i++) {
		var imageUrl = response.getValue("imageUrl", i);
		var name = response.getValue("name", i);
		var price = response.getValue("price", i);
		var costPrice = response.getValue("costPrice", i);
		var memo = response.getValue("memo", i);
		var count = response.getValue("count", i);
		if (qmsk.isEmpty(count)) {
			count = 0;
		}
		var goodsId = response.getValue("goodsId", i);
		var saleIntegral = response.getValue("saleIntegral", i);

		var $dl = $("<dl class='shopInside'></dl>");
		$dl.unbind('click');
		$dl.bind('click', {
			goodsId : goodsId
		}, caseGoos);
		var $dt = $("<dt><img src='" + imageUrl + "'>");
		$dl.append($dt);
		var $dd = $("<dd></dd>");
		var $strong = $("<strong class='title'>" + name + "</strong>");
		$dd.append($strong);
		var $p = $("<p>" + memo + "</p>");
		$dd.append($p);
		//var $b = $("<b><span class='red dolor'>￥" + price + "</span></b> / <del>市场价 " + costPrice + "</del><br />");
		var $b1 = $("<span>兑换积分 " + saleIntegral + "</span>");
		//$dd.append($b);
		$dd.append($b1);
		var $div = $("<div class='buyNumber'>已有<span class='red'>" + count + "</span>人购买</div>");
		$dd.append($div);
		$dl.append($dd);
		$("#goodsList").append($dl);
	}
	format();
}

/**
 * 点击商品后到达商品详情页面
 */
function caseGoos(event) {
	var goodsId = event.data.goodsId;
	var url = "buy_products.html";
	var data = {
		shopId : _shopId,
		openId : _openId,
		goodsId : goodsId
	};
	qmsk.redirectUrl(url, data);
}

/**
 * 加载购物车 数量
 */
function InitCount() {
	qmsk.get('../cart/cartNums/' + _openId, function(response) {
		var count = response.getValue('count');
		if (count > 0) {
			$("#sup").text("(" + count + ")");
		} else {
			$("#sup").text('');
		}
	});
}

/**
 * 截取字符串
 * @author T
 */
function format(className, num1, n, num2, m) {
	$("." + className).each(function() {
		$(this).find("p").each(function() {
			var str = $(this).html();
			title = $(this).siblings(".title");
			if (str.length > num1) {
				var newTxt = str.substring(0, n) + "…";
				$(this).html(newTxt);
			}
			if (title.length != 0) {
				if (title.html().length > num2) {
					var newTxt = title.html().substring(0, m) + "…";
					title.html(newTxt);
				}
			}
		});
	});
}

/**
 * 到购物车页面
 */
function toBuyCar() {
	qmsk.redirectUrl('buy_car.html', {
		shopId : _shopId,
		openId : _openId
	});
}

/**
 * 刷新
 */
function myrefresh() {
	window.location.reload();
}

/**
 * 商品列表页面
 */
function toOrderForm() {
	qmsk.redirectUrl('order_form.html', {
		shopId : _shopId,
		openId : _openId
	});
}

/**
 * 商品列表页面
 */
function toProducts() {
	qmsk.redirectUrl('products.html', {
		shopId : _shopId,
		openId : _openId
	});
}

/**
 * 分类选择器1
 * @param {Object} id
 * @author L
 */
function chooseGoodsTypeID(id) {
	_goodsMedId = id;
	$("#goodsList").empty();
	search();
	closeGoodsTypeChooseDiv();

}

/**
 * 分类选择器2
 * @param {Object} id
 * @author L
 */
function chooseGoodsTypeAll() {
	_goodsMedId = '';
	$("#goodsList").empty();
	search();
	closeGoodsTypeChooseDiv();
}